//
//  SASingeleCodeVC.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/5/31.
//

#import "SASingeleCodeVC.h"
#import "SASingeleCodeModel.h"
#import "SASingeleCodeCell.h"
#import "ScanHelper.h"

@interface SASingeleCodeVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray<SASingeleCodeModel *> *marr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *confirmBtn;

@end

@implementation SASingeleCodeVC

-(instancetype)initWithSingeleCodeModelMarr:(NSMutableArray<SASingeleCodeModel *> *)marr {
    if (self = [super init]) {
        self.marr = marr;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}


#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.marr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.marr[section].skuMarr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WeakSelf
    SASingeleCodeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SASingeleCodeCell" forIndexPath:indexPath];
    SASkuFormModel *model = self.marr[indexPath.section].skuMarr[indexPath.row];
    cell.model = model;
    cell.scanBlock = ^{
        [weakSelf clickedScan:model];
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(45);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return kScaleNum(5);
}

#pragma mark - event
-(void)clickedScan:(SASkuFormModel *)model {
    [self.navigationController pushViewController:[[ScanHelper shareInstance] ScanVCWithStyle:notSquare qrResultCallBack:^(id result) {
        model.barcode = result;
        [self.tableView reloadData];
        [self.navigationController popViewControllerAnimated:NO];
    }] animated:YES];
}

-(void)clickedConfirmBtn {
    [self.view endEditing:YES];
    !self.confirmBlock ?: self.confirmBlock(self.marr);
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - initUI
-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:@"单品条码"];
    [self setBgImage:nil];
    [self addRightViews:@[self.confirmBtn]];

    UIView *grayView = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight), kScreenWidth, kScaleNum(40))];
    grayView.backgroundColor = kTextBgColor;
    [self.view addSubview:grayView];
    NSArray *arr2 = @[@"颜色", @"尺码", @"条码", @"扫码"];
    for (int i = 0; i < arr2.count; i++) {
        UILabel *titleLab = [[UILabel alloc] init];
        titleLab.text = arr2[i];
        titleLab.font = kFont(15);
        titleLab.textColor = kTextColor;
        [grayView addSubview:titleLab];
        titleLab.textAlignment = NSTextAlignmentCenter;
        if (i == 0) {
            titleLab.frame = CGRectMake(kScaleNum(20), 0, kScaleNum(60), kScaleNum(40));
            titleLab.textAlignment = NSTextAlignmentLeft;
        }else if (i == 1) {
            titleLab.frame = CGRectMake(kScaleNum(85), 0, kScaleNum(60), kScaleNum(40));
        }else if (i == 2) {
            titleLab.frame = CGRectMake(kScaleNum(150), 0, kScaleNum(150), kScaleNum(40));
        }else if (i == 3) {
            titleLab.frame = CGRectMake(kScaleNum(305), 0, kScaleNum(60), kScaleNum(40));
        }
    }
    
    [self.view addSubview:self.tableView];
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight + 40), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight + 40)) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kBgColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SASingeleCodeCell class] forCellReuseIdentifier:@"SASingeleCodeCell"];
    }
    return _tableView;
}

-(UIButton *)confirmBtn {
    if (!_confirmBtn) {
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _confirmBtn.frame = SARectMake(254, 0, 53, 44);
        [_confirmBtn addTarget:self action:@selector(clickedConfirmBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmBtn;
}


@end
